package dS.ProjectGPS.LeicaOWI;

import javax.microedition.lcdui.*;

public class FormOwiANT extends Form implements FormOwi {
  OWIAgent owiAgent = null;

  ChoiceGroup location = new ChoiceGroup("Location", ChoiceGroup.EXCLUSIVE);
  TextField name = new TextField("Name", "", 50, TextField.ANY);
  TextField serial = new TextField("Serial", "", 50, TextField.ANY);
  TextField vertOffset = new TextField("Vert. Offset (m)", "", 50, TextField.DECIMAL);
  TextField horiOffset = new TextField("Hori. Offset (m)", "", 50, TextField.DECIMAL);

  StringItem pco = new StringItem("Phase Center Offset", "");
  TextField l1VertPCO = new TextField("L1 Vertical (m)", "", 50, TextField.DECIMAL);
  TextField l2VertPCO = new TextField("L2 Vertical (m)", "", 50, TextField.DECIMAL);
  TextField l1EastPCO = new TextField("L1 East (m)", "", 50, TextField.DECIMAL);
  TextField l1NorthPCO = new TextField("L1 North (m)", "", 50, TextField.DECIMAL);
  TextField l2EastPCO = new TextField("L2 East (m)", "", 50, TextField.DECIMAL);
  TextField l2NorthPCO = new TextField("L2 North (m)", "", 50, TextField.DECIMAL);

  public FormOwiANT(OWIAgent o) {
    super("Antenna Offsets");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {
    append(location);
    location.append("Attached", null);
    location.append("Remote", null);
    append(name);
    append(serial);
    append(vertOffset);
    append(horiOffset);
    append(pco);
    append(l1VertPCO);
    append(l2VertPCO);
    append(l1EastPCO);
    append(l1NorthPCO);
    append(l2EastPCO);
    append(l2NorthPCO);
  }

  public void update() {
    owiAgent.query(OWI.qsANT(0));

    location.setSelectedIndex(OWI.location, true);
    name.setString(OWI.antName);
    serial.setString(OWI.antSerial);
    vertOffset.setString(OWI.verticalOffset);
    horiOffset.setString(OWI.horizontalOffset);
    l1VertPCO.setString(OWI.l1VertPCO);
    l2VertPCO.setString(OWI.l2VertPCO);
    l1EastPCO.setString(OWI.l1EastPCO);
    l1NorthPCO.setString(OWI.l1NorthPCO);
    l2EastPCO.setString(OWI.l2EastPCO);
    l2NorthPCO.setString(OWI.l2NorthPCO);

  }

  public void save() {
    String msgId = "ANT";

    // Setup SET parameters
    String[] outputs = {
                       msgId,
                       "" + location.getSelectedIndex(),
                       name.getString(),
                       name.getString(),
                       vertOffset.getString(),
                       horiOffset.getString(),
                       l1VertPCO.getString(),
                       l2VertPCO.getString(),
                       l1EastPCO.getString(),
                       l1NorthPCO.getString(),
                       l2EastPCO.getString(),
                       l2NorthPCO.getString()

    };

    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputs)));
    Controller.showACKAlert(this,this,ackCode);

  }
}
